package gob.osinerg.emergencia.tag;

import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ValidateFormtag implements Tag{
	
	private Log log = LogFactory.getLog(ValidateFormtag.class);
	
	private PageContext pageContext;

	private Tag parent;

	public int doEndTag() throws JspException {
		List listaRequeridos = (List)pageContext.getAttribute("listaRequeridos");
		JspWriter out = pageContext.getOut();
		
		try{
			out.println("<script type=\"text/javascript\" language=\"javascript\">");
			if(listaRequeridos != null){
				for(int i = 0; i < listaRequeridos.size(); i++){
					Map data = (Map)listaRequeridos.get(i);
					if(i == 0){
						out.print("var fieldRequired = new Array('"+data.get("ID")+"'");
					}else{
						out.print(",'"+data.get("ID")+"'");
					}
				}
				out.print(");");
				out.println("\n");
				for(int i = 0; i < listaRequeridos.size(); i++){
					Map data = (Map)listaRequeridos.get(i);
					if(i == 0){
						out.print("var fieldDescription = new Array('"+data.get("NAME")+"'");
					}else{
						out.print(",'"+data.get("NAME")+"'");
					}
				}
				out.print(");");
				out.println("\n");
			}else{
				out.println("var fieldRequired = new Array();");
				out.println("var fieldDescription = new Array();");
			}
			out.println("</script>");
		}catch (Exception e) {
			log.error(e);
		}
		return EVAL_PAGE;
	}

	public int doStartTag() throws JspException {

		return SKIP_BODY;
	}

	public Tag getParent() {
		return parent;
	}

	public void release() {
		this.pageContext = null;
		this.parent = null;
		
	}

	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
	}

	public void setParent(Tag parent) {
		this.parent = parent;
	}
	
	
	

}
